<?
define('ROOT_PATH', realpath('./') . '/' );

include(ROOT_PATH . 'common.php');
include(ROOT_PATH . "js/FusionChartsFree/Code/PHP/Includes/FusionCharts.php");

if($_GET['type'] == 'blank')
	exit;

if($_GET['type'] == 'waiting')
	die('waiting ... ');


if(eregi('pages', $_GET['url']) ){
	$page = $_GET['url'];
	$fb_id_parts = split('/', $fb_page = str_replace(URL . 'facebook/pages/', '', $page) );
	$fb_id = intval($fb_id_parts[1]);
	$_GET['url'] = str_replace('pages/' . $fb_id_parts[0] . '/', '', $_GET['url']);
}


$url_info = $_GET['url'] . (($_GET['type'] != '') ? '?type=' . $_GET['type'] : '');
				
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);

curl_close($ch);

$content = str_replace('smg:', '', $content);
$content = str_replace('rdf:', '', $content);
$content = str_replace('cd:', '', $content);

$xml = simplexml_load_string($content);

if(eregi('facebook', $_GET['type']) ){

	if($_GET['type'] == 'facebook_timeline'){
		for($i = 1; $i <= 4; $i++){
			
			eval('$year = (string)$xml->Description->month_'.$i.'_year;');
			eval('$month = (string)$xml->Description->month_'.$i.'_month;');
			eval('$posts = (string)$xml->Description->month_'.$i.'_posts;');
			eval('$comments = (string)$xml->Description->month_'.$i.'_comments;');
			eval('$likes = (string)$xml->Description->month_'.$i.'_likes;');
			eval('$shares = (string)$xml->Description->month_'.$i.'_shares;');
						
			if($year > 0 && $month > 0)
				$data[intval($year)][intval($month)] = array(
					'posts' => $posts,
					'shares' => $shares,
					'likes' => $likes,
					'comments' => $comments,
					);
		}
		
	$strXML = "<graph caption='" . $caption . "' shownames='1' showPercentageinLabel='0' showPercentageValues='0' numberPrefix='' decimalPrecision='0' bgColor='f1f1f1'>";

	$strXML .= "<categories>";
	foreach($data as $year=>$month_data){
		foreach($month_data as $month=>$row){

			$strXML .= "<category name='" . $month . " - " . $year . "'/>";
		}
	}
	$strXML .= "</categories>";
	
	
	$strXML_parts['comments'] = "<dataset seriesName='Comments' color='4F81BD'>";
	$strXML_parts['posts'] = "<dataset seriesName='Posts' color='002060'>";
	$strXML_parts['likes'] = "<dataset seriesName='Likes' color='46C480'>";
	$strXML_parts['shares'] = "<dataset seriesName='Shares' color='9BBB58'>";
	
	foreach($data as $year=>$month_data){
		foreach($month_data as $month=>$row){
		
			$strXML_parts['comments'] .= "<set value='" . intval($row['comments']) . "'/>";
			$strXML_parts['posts'] .= "<set value='" . intval($row['posts']) . "'/>";
			$strXML_parts['likes'] .= "<set value='" . intval($row['likes']) . "'/>";
			$strXML_parts['shares'] .= "<set value='" . intval($row['shares']) . "'/>";
		}
	}

	$strXML_parts['comments'] .= "</dataset>";
	$strXML_parts['posts'] .= "</dataset>";
	$strXML_parts['likes'] .= "</dataset>";
	$strXML_parts['shares'] .= "</dataset>";


	
	$strXML .= implode("", $strXML_parts) . "</graph>";

	$html = renderChart("js/FusionChartsFree/Charts/FCF_MSColumn2D.swf", "", $strXML, "FactorySum", 500, 400);

	}

	
	if($_GET['type'] == 'facebook_likes'){
		$likes_gender_male = $male = (string)$xml->Description->likes_gender_male;
		$likes_gender_female = $female = (string)$xml->Description->likes_gender_female;
		$kuchen = true;
		$caption = 'Genders Likes';
	}
	if($_GET['type'] == 'facebook_comments'){
		$comments_gender_male = $male = (string)$xml->Description->comments_gender_male;
		$comments_gender_female = $female = (string)$xml->Description->comments_gender_female;
		$kuchen = true;
		$caption = 'Comments Likes';
	}
	if($_GET['type'] == 'facebook_posts'){
		$posts_gender_male = $male = (string)$xml->Description->posts_gender_male;
		$posts_gender_female = $female = (string)$xml->Description->posts_gender_female;
		$kuchen = true;
		$caption = 'Posts Likes';
	}

	if($kuchen){
		$strXML = "<graph caption='" . $caption . "' shownames='1' showPercentageinLabel='1' showPercentageValues='1' numberPrefix='$' decimalPrecision='1' bgColor='f1f1f1'>";
		$strXML .= "<set value='".max(intval($male),1)."' name='Male' color='4F81BD' alpha='60'/>";
		$strXML .= "<set value='".max(intval($female), 1)."' name='FeMale' color='C0504D'/>";
		$strXML .= "</graph>";

		$html = renderChart("js/FusionChartsFree/Charts/FCF_Doughnut2D.swf", "", $strXML, "FactorySum", 250, 200);
	}

}

if(eregi('twitter', $_GET['type']) ){
	
	if($_GET['type'] == 'twitter_timeline'){
	
		for($i = 1; $i <= 4; $i++){
			
			eval('$year = (string)$xml->Description->month_'.$i.'_year;');
			eval('$month = (string)$xml->Description->month_'.$i.'_month;');
			eval('$tweets = (string)$xml->Description->month_'.$i.'_tweets;');
			eval('$retweets = (string)$xml->Description->month_'.$i.'_retweets;');
			eval('$replies = (string)$xml->Description->month_'.$i.'_replies;');
			
			$data[intval($year)][intval($month)] = array(
				'tweets' => $tweets,
				'retweets' => $retweets,
				'replies' => $replies,
				);
		}
		
		$strXML = "<graph caption='" . $caption . "' shownames='1' showPercentageinLabel='0' showPercentageValues='0' numberPrefix='' decimalPrecision='0' bgColor='f1f1f1'>";
	
		$strXML .= "<categories>";
		foreach($data as $year=>$month_data){
			foreach($month_data as $month=>$row){
	
				$strXML .= "<category name='" . $month . " - " . $year . "'/>";
			}
		}
		$strXML .= "</categories>";
		
		
		$strXML_parts['tweets'] = "<dataset seriesName='Tweets' color='002060'>";
		$strXML_parts['replies'] = "<dataset seriesName='Replies' color='4F81BD'>";
		$strXML_parts['retweets'] = "<dataset seriesName='Retweets' color='46C480'>";
		
		foreach($data as $year=>$month_data){
			foreach($month_data as $month=>$row){
			
				$strXML_parts['tweets'] .= "<set value='" . intval($row['tweets']) . "'/>";
				$strXML_parts['retweets'] .= "<set value='" . intval($row['retweets']) . "'/>";
				$strXML_parts['replies'] .= "<set value='" . intval($row['replies']) . "'/>";
			}
		}
	
		$strXML_parts['tweets'] .= "</dataset>";
		$strXML_parts['retweets'] .= "</dataset>";
		$strXML_parts['replies'] .= "</dataset>";
	
		$strXML .= implode("", $strXML_parts) . "</graph>";
	
		$html = renderChart("js/FusionChartsFree/Charts/FCF_MSColumn2D.swf", "", $strXML, "FactorySum", 500, 400);
	}
	if($_GET['type'] == 'twitter_days'){

		for($i = 0; $i <= 6; $i++){
			
			eval('$value = (string)$xml->Description->day_'.$i.';');
			
			$data[$i] = $value;
		}
		
		
		$days_caption = array(
			0 => 'So.',
			1 => 'Mo.',
			2 => 'Di.',
			3 => 'Mi.',
			4 => 'Do.',
			5 => 'Fr.',
			6 => 'Sa.',
			);
		
			
		$strXML = "<graph caption='" . $caption . "' shownames='1' showPercentageinLabel='0' showPercentageValues='0' numberPrefix='' decimalPrecision='0' bgColor='f1f1f1'>";
	
		$strXML .= "<categories>";
		foreach($data as $day=>$value){
			$strXML .= "<category name='" . $days_caption[$day] . "'/>";
		}
		$strXML .= "</categories>";
		
		
		$strXML_parts['days'] = "<dataset seriesName='Tweets Summiert' color='4F81BD'>";
		
		foreach($data as $day=>$value){
			$strXML_parts['days'] .= "<set value='" . intval($value) . "'/>";
		}
	
		$strXML_parts['tweets'] .= "</dataset>";
	
		$strXML .= implode("", $strXML_parts) . "</graph>";
				
		$html = renderChart("js/FusionChartsFree/Charts/FCF_MSColumn2D.swf", "", $strXML, "FactorySum", 500, 400);

		
	
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Unbenanntes Dokument</title>
    <SCRIPT LANGUAGE="Javascript" SRC="js/FusionChartsFree/JSClass/FusionCharts.js"></SCRIPT>
</head>

<body topmargin="0" leftmargin="0" style="padding:0px; margin:0px;">
<?
echo $html;
?>
</body>
</html>